package com.zyg.manager.service.impl;

import com.zyg.manager.entity.SpecificationOptionEntity;
import com.zyg.manager.service.SpecificationOptionService;
import com.zyg.manager.vo.SpecificationOptionEntityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.Query;

import com.zyg.manager.dao.SpecificationDao;
import com.zyg.manager.entity.SpecificationEntity;
import com.zyg.manager.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {
    @Autowired
    private SpecificationOptionService specificationOptionService;

    /*查询规格信息列表*/
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //定义查询条件
        QueryWrapper<SpecificationEntity> queryWrapper = new QueryWrapper<>();
        Object key = params.get("key");
        if (key != null){
            queryWrapper.like("id",key)
                        .or()
                        .like("spec_name",key);
        }

        //开始分页带条件查询
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                queryWrapper
        );
        //返回
        return new PageUtils(page);
    }

    /*根据规格id查询出规格和规格选项*/
    @Override
    public SpecificationOptionEntityVo findById(String id) {
        //查询规格数据
        SpecificationEntity specificationEntity = this.getById(id);
        //查询规格选项数据
        SpecificationOptionEntityVo specificationOptionEntityVo = new SpecificationOptionEntityVo();
        List<SpecificationOptionEntity> specificationOptionEntityList = specificationOptionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));
        specificationOptionEntityVo.setSpecificationEntity(specificationEntity).setSpecificationOptionEntityList(specificationOptionEntityList);
        return specificationOptionEntityVo;
    }

    //添加规格和规格选项信息
    @Override
    public void insert(SpecificationOptionEntityVo specificationOptionEntityVo) {

        //添加规格信息
        this.save(specificationOptionEntityVo.getSpecificationEntity());
        //添加规格选项信息
//        specificationOptionService.saveBatch(specificationOptionEntityVo.getSpecificationOptionEntity());
        String id = specificationOptionEntityVo.getSpecificationEntity().getId();
        for (SpecificationOptionEntity spec : specificationOptionEntityVo.getSpecificationOptionEntityList()) {
            //为规格选项添加对应的规格id
            spec.setSpecId(id);
            specificationOptionService.save(spec);
        }
    }
    //修改
    @Override
    public void update(SpecificationOptionEntityVo specificationOptionEntityVo) {
        this.updateById(specificationOptionEntityVo.getSpecificationEntity());
        String id = specificationOptionEntityVo.getSpecificationEntity().getId();
        specificationOptionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",id));
        for (SpecificationOptionEntity spec:specificationOptionEntityVo.getSpecificationOptionEntityList()) {
            spec.setSpecId(id);
            specificationOptionService.save(spec);
        }
    }
    //删除
    @Override
    public void delete(String[] ids) {
        for (String id: ids) {
            this.removeById(id);
            specificationOptionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",id));
        }
    }

}